{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 单用户聊天 服务端 server\n",
    "\n",
    "from socket \n",
    "\n",
    "server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "server.bind((0.0.0.0, 8000))\n",
    "server.listen()\n",
    "sock, addr = server.accept()\n",
    "\n",
    "## 获取从客户端发送的数据\n",
    "## 一次获取1K数据\n",
    "while True:\n",
    "    data = sock.recv(1024)\n",
    "    print(data.decode(\"utf-8\"))\n",
    "    re_data = input()\n",
    "    sock.send(re_data.encode(\"utf-8\"))\n",
    "#     server.close()\n",
    "#     sock.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 单用户聊天 客户端 client\n",
    "\n",
    "from socket\n",
    "\n",
    "client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "client.connect(('127.0.0.1', 8000))\n",
    "while True:\n",
    "    \n",
    "    re_data = input()\n",
    "    client.send(re_data.encode(\"utf-8\"))\n",
    "    \n",
    "    data = client.recv(1024)\n",
    "    print(data.decode(\"utf-8\"))\n",
    "\n",
    "# client.send(\"bobby\".encode(\"utf-8\"))\n",
    "# data = client.recv(1024)\n",
    "# print(data.decode(\"utf-8\"))\n",
    "# client.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "###################################################################################\n",
    "## 多用户聊天 服务端 server\n",
    "\n",
    "from socket\n",
    "import threading\n",
    "\n",
    "server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "server.bind((0.0.0.0, 8000))\n",
    "server.listen()\n",
    "\n",
    "def handle_sock(sock, addr):\n",
    "    data = sock.recv(1024)\n",
    "    print(data.decode(\"utf-8\"))\n",
    "    re_data = input()\n",
    "    sock.send(re_data.encode(\"utf-8\"))\n",
    "\n",
    "## 获取从客户端发送的数据\n",
    "## 一次获取1K数据\n",
    "while True:\n",
    "    sock, addr = server.accept()\n",
    "    \n",
    "    # 用线程去处理新接手的连接（用户）\n",
    "    client_thread = threading.Thread(target=handle_sock, args=(sock, addr))\n",
    "    client_thread.start()\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 多用户聊天 客户端 client\n",
    "\n",
    "from socket\n",
    "\n",
    "client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "client.connect(('127.0.0.1', 8000))\n",
    "while True:\n",
    "    \n",
    "    re_data = input()\n",
    "    client.send(re_data.encode(\"utf-8\"))\n",
    "    \n",
    "    data = client.recv(1024)\n",
    "    print(data.decode(\"utf-8\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
